package com.ityuwen.mengyagushi.order.service.template;

import com.ityuwen.mengyagushi.album.client.AlbumInfoFeignClient;
import com.ityuwen.mengyagushi.album.client.TrackInfoFeignClient;
import com.ityuwen.mengyagushi.common.execption.GuiguException;
import com.ityuwen.mengyagushi.common.result.Result;
import com.ityuwen.mengyagushi.common.result.ResultCodeEnum;
import com.ityuwen.mengyagushi.model.album.AlbumInfo;
import com.ityuwen.mengyagushi.model.album.TrackInfo;
import com.ityuwen.mengyagushi.vo.order.OrderDetailVo;
import com.ityuwen.mengyagushi.vo.order.OrderInfoVo;
import com.ityuwen.mengyagushi.vo.order.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@OrderType(OrderConfirmType.ORDER_CONFIRM_TRACK)
public class TrackOrderConfirm extends OrderConfirm {

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    public TrackOrderConfirm(RedisTemplate redisTemplate) {
        super(redisTemplate);
    }

    @Override
    public void confirm(TradeVo tradeVo, OrderInfoVo orderInfoVo) {
        // 判断购买的声音数量是否大于0
        if (tradeVo.getTrackCount() <= 0) {
            throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);
        }

        // 根据声音id查询声音
        Result<TrackInfo> trackInfoResult = this.trackInfoFeignClient.getTrackInfo(tradeVo.getItemId());
        Assert.notNull(trackInfoResult, "声音订单确认页：获取声音失败");
        TrackInfo trackInfo = trackInfoResult.getData();
        Assert.notNull(trackInfoResult, "声音订单确认页：对应的声音不存在");

        // 根据专辑id查询专辑
        Result<AlbumInfo> albumInfoResult = this.albumInfoFeignClient.getAlbumInfo(trackInfo.getAlbumId());
        Assert.notNull(albumInfoResult, "声音订单确认页：获取专辑信息失败！");
        AlbumInfo albumInfo = albumInfoResult.getData();
        Assert.notNull(albumInfoResult, "声音订单确认页：获取专辑信息失败！");
        BigDecimal price = albumInfo.getPrice(); // 声音的单价

        // 计算订单相关价格参数
        BigDecimal originAmount = price.multiply(new BigDecimal(tradeVo.getTrackCount()));
        BigDecimal orderAmount = originAmount;
        BigDecimal derateAmount = new BigDecimal("0.00");
        orderInfoVo.setOriginalAmount(originAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        orderInfoVo.setOrderAmount(orderAmount);

        // 查询购买的声音列表
        Result<List<TrackInfo>> buyTracksResult = this.trackInfoFeignClient.findBuyTracks(tradeVo.getItemId(), tradeVo.getTrackCount());
        Assert.notNull(buyTracksResult, "声音订单确认页：获取本次购买的声音列表失败！");
        List<TrackInfo> trackInfos = buyTracksResult.getData();
        if (CollectionUtils.isEmpty(trackInfos)) {
            throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);
        }

        // 组装订单详情
        orderInfoVo.setOrderDetailVoList(trackInfos.stream().map(track -> {
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setItemId(track.getId());
            orderDetailVo.setItemName(track.getTrackTitle());
            orderDetailVo.setItemUrl(track.getCoverUrl());
            orderDetailVo.setItemPrice(price);
            return orderDetailVo;
        }).collect(Collectors.toList()));

        // 声音订单没有优惠
    }
}
